package cn.lingyangwl.agile.member.controller.admin;

import cn.lingyangwl.agile.member.assembly.MemberLevelAssembly;
import cn.lingyangwl.agile.member.model.rqrs.level.MemberLevelResp;
import cn.lingyangwl.agile.member.model.rqrs.level.MemberLevelSaveReq;
import cn.lingyangwl.agile.member.model.rqrs.level.MemberLevelUpdateReq;
import cn.lingyangwl.agile.member.service.MemberLevelService;
import cn.lingyangwl.agile.ram.ActionType;
import cn.lingyangwl.agile.ram.RamResource;
import cn.lingyangwl.framework.core.response.Resp;
import cn.lingyangwl.framework.data.mate.annotations.FieldBindHandle;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 会员等级
 * 
 * @author shenguangyang
 */
@Api(tags = "会员等级")
@RestController
@RequestMapping("/member/level")
public class MemberLevelController {
    @Resource
    private MemberLevelService memberLevelService;
    @Resource
    private MemberLevelAssembly memberLevelAssembly;

    @ApiOperation(value = "分页会员等级")
    @GetMapping("/list")
    @RamResource(action = "member:level:query", name = "查询会员等级", type = ActionType.READ)
    @FieldBindHandle
    public Resp<List<MemberLevelResp>> listMemberLevel() {
        return Resp.ok(memberLevelService.listLevel().stream().map(memberLevelAssembly::toLevelResp).collect(Collectors.toList()));
    }

    @ApiOperation(value = "通过id查找会员等级")
    @GetMapping("/get")
    @RamResource(action = "member:level:query", name = "查询会员等级", type = ActionType.READ)
    @FieldBindHandle
    public Resp<MemberLevelResp> getMemberLevel(@RequestParam(value = "id") Long id) {
        return Resp.ok(memberLevelAssembly.toLevelResp(memberLevelService.getById(id)));
    }
  
    @ApiOperation(value = "保存会员等级")
    @PostMapping("/save")
    @RamResource(action = "member:level:save", name = "保存会员等级", type = ActionType.WRITE)
    public Resp<?> saveMemberLevel(@Validated @RequestBody MemberLevelSaveReq req) {
        return Resp.ok(memberLevelService.saveLevel(req));
    }
  
    @ApiOperation(value = "更新会员等级")
    @PostMapping("/update")
    @RamResource(action = "member:level:update", name = "更新会员等级", type = ActionType.WRITE)
    public Resp<?> updateMemberLevel(@Validated @RequestBody MemberLevelUpdateReq req) {
        return Resp.ok(memberLevelService.updateLevel(req));
    }
  
    @ApiOperation(value = "批量删除会员等级")
    @DeleteMapping("/batchDelete")
    @RamResource(action = "member:level:delete", name = "删除会员等级", type = ActionType.WRITE)
    public Resp<?> batchDeleteMemberLevel(@RequestParam(value = "ids") List<Long> ids) {
        return Resp.ok(memberLevelService.batchDeleteLevel(ids));
    }
  
    @ApiOperation(value = "删除会员等级")
    @DeleteMapping("/delete")
    @RamResource(action = "member:level:delete", name = "删除会员等级", type = ActionType.WRITE)
    public Resp<?> deleteMemberLevel(@RequestParam(value = "id") Long id) {
        return Resp.ok(memberLevelService.deleteLevel(id));
    }
}
